<template>
	<view>
		<!-- 选择券 -->
		<view v-if="loading && select.length > 0" class='card' v-for="(item, index) in select" :key="index"
			:style="{background: get_back(index, item),backgroundSize: '100% 100%'}" style="border-radius: 15rpx;">
			<view @tap="card_click(item.wallet_type.id, item.type)">

				<view class="gui-card-desc">
					<view class="gui-card-desc-text">
						{{item.coupon == null ? item.wallet_type.name : ''}}
					</view>
				</view>
			</view>
		</view>
		<view style="text-align: center; margin-top: 350rpx;" v-if="select.length <= 0">暂无卡券</view>

		<menuBar menu_index="2" />
	</view>
</template>

<script>
	import QRCode from '@/Grace6/js/qrcode.js';

	import menuBar from './menu_bar.vue';
	export default {
		name: "selectcard",
		components: {
			menuBar
		},
		data() {
			return {
				loading: false,
				select: uni.getStorageSync("select"),
				user: null,
				isEmpty: false,
				sign: null,
				ZqGqTime: false,
				list: [],
			}
		},
		created() {
			let self = this
			uni.showLoading({
				title: '加载中...'
			});
			self.user = uni.getStorageSync('member')
			// self.signature();
			self.get_all_card();
			// self.getTime();
		},
		methods: {
			// 获取当前时间，判断选择券是否显示
			getTime() {
				let self = this
				let currentDate = new Date();
				let ZqGqStartTime = new Date('2024/08/06 00:00:00');
				let ZqGqEndTime = new Date('2024/09/30 00:00:00');
				if (currentDate >= ZqGqStartTime && currentDate <= ZqGqEndTime) {
					// 在时间范围内，显示卡券
					console.log('在在在');
					self.ZqGqTime = true
				} else {
					// 不在时间范围内，隐藏卡券
					console.log('不在不在不在');
					self.ZqGqTime = false
				}
			},
			// 获取用户是否签名
			signature() {
				let self = this
				self.$boya.NPost("/app/user_vip/user_sign", {
					// lg_id: self.user.lg_id // app
					lg_id: self.user.username // h5
				}, function(res) {
					uni.hideLoading();
					console.log('正确，有签名: ', res);
					uni.navigateTo({
						url: '/pages/card/wugong/indexWy'
					})
					// self.sign = res.data.sign
					// uni.setStorageSync("member_sign", res.data.sign);
				}, function(err) {
					uni.hideLoading();
					console.log('错误，无签名: ', err);
					uni.showModal({
						title: '提示',
						content: '请签名确认，确认后方可选择物供',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/member/f-signature?list=1'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					// self.sign = null
				}, {
					showError: false
				})
			},
			// 跳转到选择方案的页面
			card_click(wallet_type_id, type) {
				let self = this
				console.log('wallet_type_id: ', wallet_type_id);
				if (wallet_type_id == '50' || wallet_type_id == '57' || wallet_type_id == '84') {
					// 退休慰问
					uni.navigateTo({
						url: '/pages/activities/welfare/retire/index'
					})
				} else if (wallet_type_id == '70') {
					// 五一物供
					uni.showLoading({
						title: '正在获取签名...'
					});
					self.signature();
				} else if (wallet_type_id == '62' || wallet_type_id == '61') {
					// 先进奖品
					uni.navigateTo({
						url: '/pages/activities/welfare/advanced/index'
					})
				} else if (wallet_type_id == '65' || wallet_type_id == '83') {
					// 广钢蛋糕券
					uni.navigateTo({
						url: '/pages/card/cake?type=2025'
					})
				} else {
					uni.showToast({
						title: '暂未开放',
						icon: 'error',
						duration: 3000
					});
				}
				// uni.navigateTo({
				// 	url: '/pages/card/select/selectIndex?card_id=' + card_id + '&wallet_type_id=' +
				// 		wallet_type_id + '&type=' + type
				// })
			},
			// 排序
			sort_asc(data) {
				this.select = data.sort((a, b) => a.wallet_type.orderby - b.wallet_type.orderby)
				this.select.push()
			},
			// 获取卡包请求方法(删选出选择券)
			get_all_card() {
				let self = this;
				this.$boya.NPost("/app/card_bag/get_all", {
					type: 0
				}, function(res) {
					if (res.code == 200) {

						self.select = []
						let card = res.data.wallet
						self.loading = true;
						for (var i = 0; i < card.length; i++) {
							let idx = i
							// let startData = new Date(card[i].start_time).getTime();
							// let currentDate = Date.now();
							// if (startData > currentDate) {
							// 	card.splice(idx, 1);
							// }
							if (card[i].wallet_type.category == "选择券") {
								self.select.push(card[i])
							}
						}
						// self.select = res.data.wallet
						self.sort_asc(self.select)
						uni.setStorageSync("select", res.data.wallet)
						console.log("需要选择的券：", self.select);
					} else {
						uni.showToast({
							icon: 'error',
							title: res.data
						});
					}
				}, null, {
					showLoading: false
				});
			},
			//无背景图，匹配背景图
			get_back(index, item) {
				if (item.hasOwnProperty('coupon')) {
					if (item.coupon && item.coupon.img) {
						return "url(" + item.coupon.img + ")";
					}
				}
				index = index % 3;
				//没有背景图下方随机背景
				if (index == 1) {
					return 'url(https://boyaltd.oss-cn-hangzhou.aliyuncs.com/2022-05/09/03.png)';
				} else if (index == 2) {
					return 'url(https://boyaltd.oss-cn-hangzhou.aliyuncs.com/2022-05/09/01.png)';
				} else {
					return 'url(https://boyaltd.oss-cn-hangzhou.aliyuncs.com/2022-05/09/02.png)';
				}
			},
		}
	}
</script>

<style>
	.card {
		margin: 50rpx 45rpx;
	}

	.gui-card-img {
		width: 16%;
		height: 90rpx;
		top: 66rpx;
		left: 48rpx;
	}

	.gui-card-desc {
		color: white;
		width: 85%;
		margin: 0 auto;
		margin-top: 50rpx;
		height: 260rpx;
		line-height: 70rpx;
	}

	.gui-card-desc-text {
		text-align: center;
		color: white;
		font-size: 38rpx;
		padding-top: 82rpx;
	}
</style>